<?php
// +----------------------------------------------------------------------
// | YBlog
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://www.yhustc.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yhustc <yhustc@gmail.com>
// +----------------------------------------------------------------------
// $Id$

/**
 +------------------------------------------------------------------------------
 * YBlog基础模块 加载每个页面都需要显示的内容 定义空操作
 +------------------------------------------------------------------------------
 * @author    yhustc <yhustc@gmail.com>
 * @version   $Id$
 +------------------------------------------------------------------------------
 */

class BaseAction extends Action
{
	/**
     +----------------------------------------------------------
     * 选项配置数组
     +----------------------------------------------------------
     * @var array
     * @access protected
     +----------------------------------------------------------
     */
	protected $options;

	 /**
	 +----------------------------------------------------------
	 * 空操作,对不存在的页面进行报错处理
	 +----------------------------------------------------------
	 * @access public
	 +----------------------------------------------------------
	 * @return void
	 +----------------------------------------------------------
	 */
	function _empty()
	{
		$this->error("请求的页面不存在");
	}
	
	 /**
	 +----------------------------------------------------------
	 * 初始化,加载并缓存用户设置
	 +----------------------------------------------------------
	 * @access public
	 +----------------------------------------------------------
	 * @return void
	 +----------------------------------------------------------
	 */
	function _initialize()
	{
		$this->options = S("options");
		if(!$this->options)
		{
			//由于修改了设置,缓存的用户设置信息被清空了
			//重新加载并缓存起来
			$optionDao = D("Options");
			$optionList = $optionDao->where("autoload='yes'")->field("option_name,option_value")->findAll();
			$this->options = $optionDao->getCols($optionList,"option_name,option_value");
			S("options",$this->options);
		}

		/*取出widget并按保存的顺序调用挂载函数*/
		$onWidget = S("widgets");
		if($onWidget===false)
		{
			$pluginDao = D("PlugIn");
			$plugins = $pluginDao->where("status=1")->field("name,file,type")->order("priority ASC")->findAll();
			// 缓存启用的插件数据
			$content = '';
			$onWidget = array();
			foreach($plugins as $plugin) {
				if($plugin['type']=='plugin')
					$content .= php_strip_whitespace($plugin['file']);
				else if($plugin['type']=='widget')
					$onWidget[] = $plugin['name']; //按照YBlog的Widget命名规范,所调用的Widget的name字段是相同的
			}
			file_put_contents(APP_PATH."/Temp/~plugins.php",$content);
			S("widgets",$onWidget);
			
			//缓存更新成功,重新显示页面
			header("location: ".$_SERVER["REQUEST_URI"]);
		}
		
		//对POST进行安全过滤
		$_POST = inputFilter($_POST);
		$this->assign("widgets",$onWidget);
		$this->assign("options",$this->options);
	}

	/**
	+----------------------------------------------------------
	* 验证码显示
	+----------------------------------------------------------
	* @access public 
	+----------------------------------------------------------
	* @return void
	+----------------------------------------------------------
	* @throws ThinkExecption
	+----------------------------------------------------------
	*/
    public function verify() 
    {
		$type =	isset($_GET['type'])?$_GET['type']:'gif';
        import("ORG.Util.Image");
        Image::buildImageVerify(4,1,$type);
    }
}
?>